//
//  SettingMgr.m
//  CarCare
//
//  Created by ileo on 15/2/9.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "SettingMgr.h"
#import "NetSetting.h"
#import "NetCar.h"

#define Tips_Detail_On @"已打开"
#define Tips_Detail_Off @"已关闭"
#define Tips_Detail_Setting @"正在设置..."
@interface SettingMgr()

@end

@implementation SettingMgr

IMP_SINGLETON

-(instancetype)init{
    self = [super init];
    if (self) {
        
        [self updateSettingInfoFinish:nil];
        self.refreshVoltage = [RefreshMgr sharedInstance].refreshVoltage;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification) name:REQUEST_FIRE_ONOFF object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification) name:REQUEST_VIBRATION_ONOFF object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification) name:REQUEST_TRIP_ONOFF object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification) name:REQUEST_VOLTAGE_ONOFF object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification) name:REQUEST_TRAJECTORY_ONOFF object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification) name:REQUEST_VOLTAGE_VALUE object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification) name:REQUEST_GESTURE_ONOFF object:nil];
        
    }
    return self;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -

-(void)notification{
    [self updateSettingInfoFinish:nil];
}

-(void)updateSettingInfoFinish:(void (^)())finish{
    __weak __typeof(self) wself = self;
    [[NetSetting getSettingInfo] requestSuccess:^(id JSON) {
        wself.showTipsFire = [JSON[@"fireSwitch"] integerValue] == 1;
        wself.showTipsShake = [JSON[@"shakeSwitch"] integerValue] == 1;
        wself.showTipsTravel = [JSON[@"tripSwitch"] integerValue] == 1;
        
        wself.showFireDetail = wself.showTipsFire ? Tips_Detail_On : Tips_Detail_Off;
        wself.showShakeDetail = wself.showTipsShake ? Tips_Detail_On : Tips_Detail_Off;
        wself.showTravelDetail = wself.showTipsTravel ? Tips_Detail_On : Tips_Detail_Off;
        
        wself.showTipsVoltage = [JSON[@"voltageAlarmFlag"] integerValue] == 1;
        wself.voltageValue = [JSON[@"voltageAlarmValue"] floatValue];
        
        wself.showTipsTrajectory = [JSON[@"tripOrbitSwitch"] integerValue] == 1;
        wself.showGesturePassword = [JSON[@"guesturePasswordFlag"] integerValue] == 1;
        wself.showCanLookGesturePW = [JSON[@"showGuestureOrbitFlag"] integerValue] == 1;
        wself.gesturePWStr = JSON[@"guestureValue"];
        if (finish) {
            finish();
        }
    }];

}

#pragma mark - 获取手势密码开关状态 用来刷新手势密码 防止不同手机
- (void)getGuestureInfoFinish:(void(^)())finish
{
    __weak __typeof(self) wself = self;
    [[NetSetting getGustureInfoSetting]requestSuccess:^(id JSON) {
        wself.showGesturePassword = [JSON[@"guesturePasswordFlag"] integerValue] == 1;
        wself.showCanLookGesturePW = [JSON[@"showGuestureOrbitFlag"] integerValue] == 1;
        wself.gesturePWStr = JSON[@"value"];
        if (finish) {
            finish();
        }
    }failure:^(id JSON) {
        
    }];
}

#pragma mark - onoff

-(void)setTipsFire:(BOOL)show finish:(void(^)())finish failure:(void(^)())failure{
    self.showTipsFire = show;
    self.showFireDetail = Tips_Detail_Setting;
    __weak __typeof(self) wself = self;
    [NSTimer scheduledTimerWithTimeInterval:0.3 block:^{
        [NetSetting setFire:show finish:^{
            wself.showFireDetail = show ? Tips_Detail_On : Tips_Detail_Off;
            if(finish)finish();
        } failure:^{
            wself.showTipsFire = !wself.showTipsFire;
            wself.showFireDetail = wself.showTipsFire ? Tips_Detail_On : Tips_Detail_Off;
            if(failure)failure();
        }];
    } repeats:NO];
    
}

-(void)setTipsShake:(BOOL)show finish:(void(^)())finish failure:(void(^)())failure{
    self.showTipsShake = show;
    self.showShakeDetail = Tips_Detail_Setting;
    __weak __typeof(self) wself = self;
    [NSTimer scheduledTimerWithTimeInterval:0.3 block:^{
        [NetSetting setVibration:show finish:^{
            wself.showShakeDetail = show ? Tips_Detail_On : Tips_Detail_Off;
            if(finish)finish();
        } failure:^{
            wself.showTipsShake = !wself.showTipsShake;
            wself.showShakeDetail = wself.showTipsShake ? Tips_Detail_On : Tips_Detail_Off;
            if(failure)failure();
        }];
    } repeats:NO];
}

-(void)setTipsTravel:(BOOL)show finish:(void(^)())finish failure:(void(^)())failure{
    self.showTipsTravel = show;
    self.showTravelDetail = Tips_Detail_Setting;
    __weak __typeof(self) wself = self;
    [NSTimer scheduledTimerWithTimeInterval:0.3 block:^{
        [NetSetting setTrip:show finish:^{
            wself.showTravelDetail = show ? Tips_Detail_On : Tips_Detail_Off;
            if(finish)finish();
        } failure:^{
            wself.showTipsTravel = !wself.showTipsTravel;
            wself.showTravelDetail = wself.showTipsTravel ? Tips_Detail_On : Tips_Detail_Off;
            if(failure)failure();
        }];
    } repeats:NO];
}

-(void)setTipsVoltage:(BOOL)show{
    self.showTipsVoltage = show;
    [NetSetting setVoltage:show];
}

-(void)setShowVoltage:(CGFloat)voltage{
    self.voltageValue = voltage;
    [NetSetting setVoltageValue:[NSString stringWithFormat:@"%0.2f",voltage]];
}
//添加轨迹通知开关
-(void)setTipsTrajectory:(BOOL)show
{
    self.showTipsTrajectory = show;
    [NetSetting setTrajectory:show];
}
//设置手势密码开关
- (void)setGesturePasswordFlag:(BOOL)gesturePWShow withCanLookGesturePWFLag:(BOOL)canLookGesturePWShow withGesturePWStr:(NSString *)gesturePWStr
{
    self.showGesturePassword = gesturePWShow;
    self.showCanLookGesturePW = canLookGesturePWShow;
    self.gesturePWStr = gesturePWStr;
    [NetSetting setGesturePWSwitch:gesturePWShow withCanLookGesturePW:canLookGesturePWShow withGesturePWStr:gesturePWStr];
}

-(NSArray *)carCityNames{
    return @[@"京",@"津",@"沪",@"渝",@"冀",@"豫",@"云",@"辽",@"黑",@"湘",@"皖",@"鲁",@"新",@"苏",@"浙",@"赣",@"鄂",@"桂",@"甘",@"晋",@"蒙",@"陕",@"吉",@"闽",@"贵",@"粤",@"川",@"青",@"藏",@"琼",@"宁"];
}

@end
